function  [bdrawp,m1,CC1,rejcount]=drawnsta(y,x,prm,prprec,sig);
%function [bdrawp,m1,CC1]=drawnsta(y,x,prm,prprec,sig); 
% Draw Coefficients from a Normal Distribution 
% Until a stationary draw is attained 
%
% If do not wish to impose stationarity use drawnres.m 
% 
% Prior ~ Normal( prm, prprec^(-1) )  
%      prprec is the precision  
% y      (Txn) 
% x      (TxK)  
% Prm    (Kx1)  Prior Mean 
% prprec (KxK)  Prior Precision
% Sig    (1x1)
%
% bdrawp (Kx1)is the draw from the posterior normal as a row vector 
% m1     (Kx1) is the posterior mean 
% CC1     (KxK) is the Cholelsky of the posterior variance 
%         V1= CC1'*CC1
%
%rejcount     Number of tries needed to obtain a stationary draw 
%
[ty,ny]=size(y); 
[tx,nx]=size(x); 
nk=length(prm);
if tx~=ty
    error('Not equal number of observations in drawcoef'); 
end; 
V1=( prprec+( (1/sig)*(x'*x) ) ) \ eye(nk) ; % Posterior Variance  
m1=V1*(prprec*prm + (1/sig)*(x'*y) );        % Posterior Mean  
CC1=chol(V1); 
acc=0;  
rejcount=0; 
while acc < 1;     
    bdraw=m1+CC1'*randn(nk,1);
    bdrawp=bdraw'; 
    if min( abs( rootpol([1 bdrawp]) ) ) >=1.0001; 
        acc=1; 
    else
        rejcount=rejcount+1; 
    end
end